<article class="" id="post-">
<header class="entry-header">
<h1 class="entry-title">jQuery.merge()</h1>
<div class="entry-meta">
						所属分类：<span class="category"><a href="/category/utilities/">工具类</a></span>
<span class="pull-right">英文文档：<a href="https://api.jquery123.com/jQuery.merge/" target="_blank">jQuery.merge()</a></span>
</div><!-- .entry-meta -->
</header><!-- .entry-header -->
<!-- .entry-header -->
<div class="entry-content">
<article class="entry method" id="jQuery-merge1"><h2 class="section-title">
<span class="name">jQuery.merge( first, second )</span><span class="returns">返回: <a href="/Types/#Array">Array</a></span>
</h2>
<div class="entry-wrapper">
<p class="desc"><strong>描述: </strong>合并两个数组内容到第一个数组。 </p>
<ul class="signatures"><li class="signature" id="jQuery-merge-first-second">
<h4 class="name">
<span class="version-details">添加的版本: <a href="/category/version/1.0/">1.0</a></span>jQuery.merge( first, second )</h4>
<ul>
<li>
<div><strong>first</strong></div>
<div>类型: <a href="/Types/#Array">Array</a>
</div>
<div>第一个用于合并的数组，其中将会包含合并后的第二个数组的内容。</div>
</li>
<li>
<div><strong>second</strong></div>
<div>类型: <a href="/Types/#Array">Array</a>
</div>
<div>第二个用于合并的数组，该数组不会被修改，其中的内容将会被合并到第一个数组中。</div>
</li>
</ul>
</li></ul>
<div class="longdesc" id="entry-longdesc">
<p><code>$.merge()</code>操作形成一个数组，其中包含两个数组的所有元素。追加到第一个数组中的第二个数组元素的顺序会被保留。<code>$.merge()</code>函数是破坏性的。它会修改第一个数组的内容，并将第二个数组的内容添加到第一个数组中。  </p>
<p>如果您需要保留原始的第一个数组，请在调用<code>$.merge()</code>前拷贝一个出来。幸运的是， <code>$.merge()</code>本身也可以用于拷贝操作：</p>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> newArray = $.merge([], oldArray);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<p>此快捷方式创建一个新的，空数组合并了oldArray的内容，有效地克隆了数组。</p>
<p>在jQuery 1.4之前，该参数应该是原生的Javascript数组对象;如果情况并非如此（参数不是原生的Javascript数组对象）请使用<code>$.makeArray</code>将它们变成真正的 Javascript 数组。</p>
</div>
<section class="entry-examples" id="entry-examples"><header><h2 class="underline">例子:</h2></header><div class="entry-example" id="example-0">
<h4>Example: <span class="desc">合并两个数组，修改第一个参数的内容。</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.merge( [<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>], [<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>] )</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Result:</h4>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>[<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>] </code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-1">
<h4>Example: <span class="desc">合并两个数组，修改第一个参数的内容。</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>$.merge( [<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>], [<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>] )  </code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Result:</h4>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>[<span class="number">3</span>,<span class="number">2</span>,<span class="number">1</span>,<span class="number">4</span>,<span class="number">3</span>,<span class="number">2</span>] </code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="entry-example" id="example-2">
<h4>Example: <span class="desc">合并两个数组，但是对第一个参数进行拷贝，这样就可以保证原始的内容不被修改。</span>
</h4>
<div class="syntaxhighlighter javascript ">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
<div class="line n2">2</div>
<div class="line n3">3</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code><span class="keyword">var</span> first = [<span class="string">'a'</span>,<span class="string">'b'</span>,<span class="string">'c'</span>];</code></div></div><div class="container"><div class="line"><code><span class="keyword">var</span> second = [<span class="string">'d'</span>,<span class="string">'e'</span>,<span class="string">'f'</span>];</code></div></div><div class="container"><div class="line"><code>$.merge( $.merge([],first), second);</code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
<h4>Result:</h4>
<div class="syntaxhighlighter javascript nogutter">
<table>
<tbody>
<tr>
<td class="gutter">
<div class="line n1">1</div>
</td>
<td class="code">
<pre><div class="container"><div class="line"><code>[<span class="string">"a"</span>,<span class="string">"b"</span>,<span class="string">"c"</span>,<span class="string">"d"</span>,<span class="string">"e"</span>,<span class="string">"f"</span>] </code></div></div></pre>
</td>
</tr>
</tbody>
</table>
</div>
</div></section>
</div></article> </div>
</article>